package com.xhd.db;

import java.text.SimpleDateFormat;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.util.Date;

public class DBUtil {
	 private static Hashtable jm469 = null;
	  public static final int UNKNOWN = 0;
	  public static final int ORACLE = 1;
	  public static final int SQL_SERVER = 2;
	  public static final int INFORMIX = 3;
	  public static final int SYBASE = 4;
	  public static final int ACCESS = 5;
	  public static final int D_BASE = 6;
	  public static final int PARADOX = 7;
	  public static final int EXCEL = 8;
	  public static final int TEXT = 9;
	  public static final int MYSQL = 10;
	  public static final int STR_LEN = 1;
	  public static final int STR_SUB = 2;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	     
	      try
	      {
	       DBUtil.jm10 = Class.forName("com.sinocc.systemdbmanager.DBUtil");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  private DBUtil()
	  {
	    jm469 = new Hashtable();
	    String[] arrayOfString = new String[4];
	    arrayOfString[0] = "length";
	    arrayOfString[1] = "len";
	    arrayOfString[2] = "";
	    arrayOfString[3] = "";
	    jm469.put(new Integer(1), arrayOfString);
	    arrayOfString = new String[4];
	    arrayOfString[0] = "substr";
	    arrayOfString[1] = "substring";
	    arrayOfString[2] = "";
	    arrayOfString[3] = "";
	    jm469.put(new Integer(2), arrayOfString);
	  }

	  public static String genLimitRowSQL(int paramInt1, String paramString, int paramInt2)
	  {
	    if ((paramInt2 < 1) || (paramString == null) || (paramString.length() < 1))
	      return paramString;
	    if (getKeyIndex(paramString, "select ", 0) != 0)
	      return paramString;
	    int i = 0;
	    int j;
	    switch (paramInt1)
	    {
	    case 1:
	      i = getKeyIndex(paramString, "where ", 0);
	      if (i > 0)
	      {
	        if ((getKeyIndex(paramString, "rownum>", i += 5) > 0) || (getKeyIndex(paramString, "rownum>", i) > 0) || (getKeyIndex(paramString, "rownum=", i) > 0))
	          return paramString;
	        String str1 = paramString;
	        paramString = str1.substring(0, i);
	        paramString = paramString + " rownum<" + String.valueOf(paramInt2) + " AND";
	        paramString = paramString + str1.substring(i, str1.length());
	        return paramString;
	      }
	      j = getKeyIndex(paramString, "order by ", 0);
	      int k = getKeyIndex(paramString, "group by ", 0);
	      i = (k < j) ? k : j;
	      if (i > 0)
	      {
	        String str2 = paramString;
	        paramString = str2.substring(0, i);
	        paramString = paramString + "WHERE rownum<" + String.valueOf(paramInt2);
	        paramString = paramString + str2.substring(i - 1, str2.length());
	      }
	      else
	      {
	        paramString = paramString + "WHERE rownum<" + String.valueOf(paramInt2);
	      }
	      return paramString;
	    case 2:
	      if (getKeyIndex(paramString, "top ", 0) > 0)
	        return paramString;
	      i = getKeyIndex(paramString, "select ", 0);
	      if (i < 0)
	        return paramString;
	      for (j = i += 6; j < paramString.length(); ++j)
	      {
	        if (paramString.charAt(j) == ' ')
	          continue;
	        if ((j + 10 >= paramString.length()) || ("distinct ".compareToIgnoreCase(paramString.substring(j, j + 9)) != 0))
	          break;
	        i = j + 8;
	        break;
	      }
	      paramString = paramString.substring(0, i) + " top " + String.valueOf(paramInt2) + paramString.substring(i, paramString.length());
	      return paramString;
	    case 3:
	    case 4:
	    }
	    return paramString;
	  }

	  public static String getDate(int paramInt)
	  {
	    switch (paramInt)
	    {
	    case 1:
	      return "sysdate";
	    case 2:
	      return "getdate()";
	    case 3:
	      return "";
	    case 4:
	      return "";
	    }
	    return "";
	  }

	  public static String getDateFormat(int paramInt, Date paramDate)
	  {
	    String str = "yyyy-MM-dd HH:mm:ss";
	    switch (paramInt)
	    {
	    case 1:
	      str = "MM/dd/yyyy HH:mm:ss";
	    }
	    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(str);
	    return localSimpleDateFormat.format(paramDate);
	  }

	  public static int getDBType(String paramString)
	  {
	    if (paramString != null)
	    {
	      String str = paramString.toLowerCase();
	      if (str.indexOf("oracle") != -1)
	        return 1;
	      if ((str.indexOf("microsoft sql server") != -1) || (str.indexOf("sqlserver") != -1))
	        return 2;
	      if (str.indexOf("informix") != -1)
	        return 3;
	      if (str.indexOf("sybase") != -1)
	        return 4;
	      if (str.indexOf("access") != -1)
	        return 5;
	      if (str.indexOf("parabox") != -1)
	        return 7;
	      if (str.indexOf("dbase") != -1)
	        return 6;
	      if (str.indexOf("excel") != -1)
	        return 8;
	      if (str.indexOf("text") != -1)
	        return 9;
	      if ((str.indexOf("mysql") >= 0) || (str.indexOf("my sql") >= 0))
	        return 10;
	    }
	    jm9.info("Warning: unknown database type: " + paramString);
	    return 0;
	  }

	  public static String getDiffDays(int paramInt, String paramString1, String paramString2)
	  {
	    switch (paramInt)
	    {
	    case 1:
	      return paramString2 + "-" + paramString1;
	    case 2:
	      return "DATEDIFF(day," + paramString1 + "," + paramString2 + ")";
	    case 3:
	      return "";
	    case 4:
	      return "";
	    }
	    return "";
	  }

	  public static String getFunction(int paramInt1, int paramInt2)
	  {
	    String[] arrayOfString = (String[])jm365().get(new Integer(paramInt2));
	    if (arrayOfString != null)
	      switch (paramInt1)
	      {
	      case 1:
	        return arrayOfString[0];
	      case 2:
	        return arrayOfString[1];
	      case 3:
	        return arrayOfString[2];
	      case 4:
	        return arrayOfString[3];
	      }
	    return "";
	  }

	  public static String getJoinStr(int paramInt1, int paramInt2, String paramString1, String paramString2)
	  {
	    String str = "";
	    switch (paramInt1)
	    {
	    case 1:
	      if (paramInt2 == 1)
	      {
	        str = paramString1 + "=" + paramString2 + "(+)";
	        break;
	      }
	      if (paramInt2 != 2)
	        break ;
	      str = paramString1 + "(+)=" + paramString2;
	      break;
	    case 2:
	    case 4:
	      if (paramInt2 == 1)
	      {
	        str = paramString1 + "*=" + paramString2;
	        break ;
	      }
	      if (paramInt2 != 2)
	        break ;
	      str = paramString1 + "=*" + paramString2;
	    case 3:
	    }
	    label175: return str;
	  }

	  public static int getKeyIndex(String paramString1, String paramString2, int paramInt)
	  {
	    if ((paramString1 == null) || (paramString2 == null) || (paramString1.length() < 1) || (paramString2.length() < 1))
	      return -1;
	    String str = paramString1.toLowerCase();
	    paramString2 = paramString2.toLowerCase();
	    int i = paramInt - 1;
	    int j = 0;
	    while (true)
	    {
	      i = str.indexOf(paramString2, i + 1);
	      if (i < 0)
	        break;
	      int k = -1;
	      for (j = 0; ; j = 1)
	        while (true)
	        {
	          k = paramString1.indexOf("'", k + 1);
	          if (k < 0)
	            break ;
	          if (k > i)
	            break ;
	          if (j == 0)
	            break;
	          j = 0;
	        }
	  
	    }
	    if (j != 0)
	      i = -1;
	    return i;
	  }

	  private static Hashtable jm365()
	  {
	    if (jm469 == null)
	    {
	      DBUtil localDBUtil = new DBUtil();
	      localDBUtil.hashCode();
	    }
	    return jm469;
	  }

	  public static String strConcat(int paramInt, String[] paramArrayOfString)
	  {
	    if (paramArrayOfString == null)
	      return "";
	    if (paramArrayOfString.length < 2)
	      return paramArrayOfString[0];
	    String str = strConcat(paramInt, paramArrayOfString[0], paramArrayOfString[1]);
	    if (paramArrayOfString.length > 2)
	      for (int i = 2; i < paramArrayOfString.length; ++i)
	        str = strConcat(paramInt, str, paramArrayOfString[i]);
	    return str;
	  }

	  public static String strConcat(int paramInt, String paramString1, String paramString2)
	  {
	    switch (paramInt)
	    {
	    case 1:
	      return paramString1 + "||" + paramString2;
	    case 2:
	      return paramString1 + "+" + paramString2;
	    case 3:
	      return "";
	    case 4:
	      return paramString1 + "+" + paramString2;
	    }
	    return "";
	  }

	  public static String toDate(int paramInt, String paramString)
	  {
	    if ((paramString == null) || (paramString.equals("''")))
	      return "null";
	    switch (paramInt)
	    {
	    case 1:
	      return "to_date(" + paramString + ",'YYYY-MM-DD HH24-MI-SS')";
	    case 2:
	      return "convert(datetime," + paramString + ")";
	    case 3:
	      return "";
	    case 4:
	      return "";
	    }
	    return "";
	  }

	  public static String toDate(int paramInt, Date paramDate)
	  {
	    String str1 = "yyyy-MM-dd HH:mm:ss";
	    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(str1);
	    String str2 = localSimpleDateFormat.format(paramDate);
	    switch (paramInt)
	    {
	    case 1:
	      return "to_date('" + str2 + "','YYYY-MM-DD HH24-MI-SS')";
	    case 2:
	      return "convert(datetime, '" + str2 + "')";
	    case 3:
	    case 4:
	    }
	    return str2;
	  }

	  public static String toDays(int paramInt, String paramString)
	  {
	    switch (paramInt)
	    {
	    case 1:
	      return "round(" + paramString + ",'HH')";
	    case 2:
	      return "CONVERT(numeric(12,1)," + paramString + ")";
	    case 3:
	      return "";
	    case 4:
	      return "";
	    }
	    return "";
	  }

	  public static String toString(int paramInt1, String paramString, int paramInt2)
	  {
	    switch (paramInt1)
	    {
	    case 1:
	      if (paramInt2 > 0)
	        return "cast(" + paramString + " as varchar2(" + paramInt2 + "))";
	      return "to_char(" + paramString + ")";
	    }
	    if (paramInt2 > 0)
	      return "cast(" + paramString + " as varchar(" + paramInt2 + "))";
	    return "cast(" + paramString + " as varchar)";
	  }

	  public static String getFieldTypeName(int paramInt, String paramString)
	  {
	    String str = "";
	    if ((paramString == null) || (paramString.length() < 1))
	      return "";
	    paramString = paramString.trim().toLowerCase();
	    switch (paramInt)
	    {
	    case 1:
	      if (paramString.equals("integer"))
	      {
	        str = "NUMBER";
	        break ;
	      }
	      if (paramString.equals("varchar"))
	      {
	        str = "VARCHAR2";
	        break ;
	      }
	      if (paramString.equals("datetime"))
	      {
	        str = "DATE";
	        break ;
	      }
	      if (!(paramString.equals("number")))
	        break ;
	      str = "NUMBER";
	      break;
	    case 2:
	      if (paramString.equals("integer"))
	      {
	        str = "int";
	        break ;
	      }
	      if (paramString.equals("varchar"))
	      {
	        str = "varchar";
	        break ;
	      }
	      if (paramString.equals("datetime"))
	      {
	        str = "datetime";
	        break ;
	      }
	      if (!(paramString.equals("number")))
	        break ;
	      str = "numeric";
	      break;
	    default:
	      return paramString;
	    }
	    label186: return str;
	  }

	  public static String converNullTo(int paramInt, String paramString1, String paramString2)
	  {
	    String str = "";
	    switch (paramInt)
	    {
	    case 1:
	      str = "DECODE(" + paramString1 + ",NULL," + paramString2 + "," + paramString1 + ")";
	      break;
	    case 2:
	    case 4:
	      str = "ISNULL(" + paramString1 + "," + paramString2 + ")";
	    case 3:
	    }
	    return str;
	  }

	  public static String[] fullJoin(int paramInt, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)
	  {
	    return fullJoin(paramInt, paramString1, paramString2, paramString3, paramString4, paramString5, paramString6);
	  }

	  public static String[] fullJoin(int paramInt, boolean paramBoolean, String paramString1, String paramString2, String paramString3)
	  {
	    return fullJoin(paramInt, paramBoolean, paramString1, paramString2, null, null, paramString3, null);
	  }

	  public static String[] fullJoin(int paramInt, boolean paramBoolean, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)
	  {
	    String str1 = "";
	    String str2 = "";
	    if ((paramString3 == null) || (paramString3.length() < 1))
	      paramString3 = paramString1;
	    if ((paramString4 == null) || (paramString4.length() < 1))
	      paramString4 = paramString2;
	    if ((paramString6 == null) || (paramString6.length() < 1))
	      paramString6 = paramString5;
	    switch (paramInt)
	    {
	    case 1:
	      if (paramBoolean)
	        str1 = "," + paramString2 + " " + paramString4;
	      else
	        str1 = paramString1 + " " + paramString3 + "," + paramString2 + " " + paramString4;
	      str2 = str2 + paramString3 + "." + paramString5 + "(+)=" + paramString4 + "." + paramString6 + " (+)";
	      break;
	    case 2:
	    case 4:
	      if (paramBoolean)
	        str1 = paramString1 + " " + paramString3;
	      str1 = str1 + " FULL JOIN " + paramString2 + " " + paramString4;
	      str1 = str1 + " ON " + paramString3 + "." + paramString5 + "=" + paramString4 + "." + paramString6;
	      str2 = "1=1";
	    case 3:
	    }
	    String[] arrayOfString = new String[2];
	    arrayOfString[0] = str1;
	    arrayOfString[1] = str2;
	    return arrayOfString;
	  }

	  public static int getInLimit(int paramInt)
	  {
	    switch (paramInt)
	    {
	    case 1:
	      return 250;
	    case 2:
	      return 1024;
	    }
	    return 1024;
	  }

	  public static String getRptColType(int paramInt1, int paramInt2)
	  {
	    String str = "";
	    switch (paramInt2)
	    {
	    case -6:
	    case -5:
	    case 4:
	    case 5:
	      str = "integer";
	      break;
	    case 91:
	    case 92:
	    case 93:
	      str = "datetime";
	      break;
	    case 2:
	    case 3:
	    case 6:
	    case 7:
	    case 8:
	      str = "number";
	      break;
	    default:
	      str = "varchar";
	    }
	    return str;
	  }

	  public static String[] leftJoin(int paramInt, String paramString1, String paramString2, String paramString3)
	  {
	    return leftJoin(paramInt, false, paramString1, paramString2, null, null, paramString3, null);
	  }

	  public static String[] leftJoin(int paramInt, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)
	  {
	    return leftJoin(paramInt, false, paramString1, paramString2, paramString3, paramString4, paramString5, paramString6);
	  }

	  public static String[] leftJoin(int paramInt, boolean paramBoolean, String paramString1, String paramString2, String paramString3)
	  {
	    return leftJoin(paramInt, paramBoolean, paramString1, paramString2, null, null, paramString3, null);
	  }

	  public static String[] leftJoin(int paramInt, boolean paramBoolean, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)
	  {
	    String str1 = "";
	    String str2 = "";
	    if ((paramString3 == null) || (paramString3.length() < 1))
	      paramString3 = paramString1;
	    if ((paramString4 == null) || (paramString4.length() < 1))
	      paramString4 = paramString2;
	    if ((paramString6 == null) || (paramString6.length() < 1))
	      paramString6 = paramString5;
	    switch (paramInt)
	    {
	    case 1:
	      if (paramBoolean)
	        str1 = "," + paramString2 + " " + paramString4;
	      else
	        str1 = paramString1 + " " + paramString3 + "," + paramString2 + " " + paramString4;
	      str2 = str2 + paramString3 + "." + paramString5 + "=" + paramString4 + "." + paramString6 + " (+)";
	      break;
	    case 2:
	    case 4:
	      if (!(paramBoolean))
	        str1 = paramString1 + " " + paramString3;
	      str1 = str1 + " LEFT JOIN " + paramString2 + " " + paramString4;
	      str1 = str1 + " ON " + paramString3 + "." + paramString5 + "=" + paramString4 + "." + paramString6;
	      str2 = "1=1";
	    case 3:
	    }
	    String[] arrayOfString = new String[2];
	    arrayOfString[0] = str1;
	    arrayOfString[1] = str2;
	    return arrayOfString;
	  }

	  public static String[] rightJoin(int paramInt, String paramString1, String paramString2, String paramString3)
	  {
	    return rightJoin(paramInt, false, paramString1, paramString2, null, null, paramString3, null);
	  }

	  public static String[] rightJoin(int paramInt, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)
	  {
	    return rightJoin(paramInt, false, paramString1, paramString2, paramString3, paramString4, paramString5, paramString6);
	  }

	  public static String[] rightJoin(int paramInt, boolean paramBoolean, String paramString1, String paramString2, String paramString3)
	  {
	    return rightJoin(paramInt, paramBoolean, paramString1, paramString2, null, null, paramString3, null);
	  }

	  public static String[] rightJoin(int paramInt, boolean paramBoolean, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5, String paramString6)
	  {
	    String str1 = "";
	    String str2 = "";
	    if ((paramString3 == null) || (paramString3.length() < 1))
	      paramString3 = paramString1;
	    if ((paramString4 == null) || (paramString4.length() < 1))
	      paramString4 = paramString2;
	    if ((paramString6 == null) || (paramString6.length() < 1))
	      paramString6 = paramString5;
	    switch (paramInt)
	    {
	    case 1:
	      if (paramBoolean)
	        str1 = "," + paramString2 + " " + paramString4;
	      else
	        str1 = paramString1 + " " + paramString3 + "," + paramString2 + " " + paramString4;
	      str2 = str2 + paramString3 + "." + paramString5 + "(+)=" + paramString4 + "." + paramString6;
	      break;
	    case 2:
	    case 4:
	      if (!(paramBoolean))
	        str1 = paramString1 + " " + paramString3;
	      str1 = str1 + " RIGHT JOIN " + paramString2 + " " + paramString4;
	      str1 = str1 + " ON " + paramString3 + "." + paramString5 + "=" + paramString4 + "." + paramString6;
	      str2 = "1=1";
	    case 3:
	    }
	    String[] arrayOfString = new String[2];
	    arrayOfString[0] = str1;
	    arrayOfString[1] = str2;
	    return arrayOfString;
	  }

	  public static String strToDate(int paramInt, Object paramObject)
	  {
	    if (paramObject == null)
	      return "NULL";
	    String str = String.valueOf(paramObject);
	    if (str.length() < 1)
	      return "null";
	    switch (paramInt)
	    {
	    case 1:
	      if (str.length() == 10)
	        return "to_date('" + str + "','YYYY-MM-DD')";
	      if (str.length() == 8)
	        return "to_date('" + str + "','HH24-MI-SS')";
	      return "to_date('" + str + "','YYYY-MM-DD HH24-MI-SS')";
	    case 2:
	      return "convert(datetime,'" + str + "')";
	    case 3:
	    case 4:
	    }
	    return "convert(datetime,'" + str + "')";
	  }

	  public static String filterStringToDB(String paramString)
	  {
	    if ((paramString == null) || (paramString.equals("")))
	      return "";
	    StringBuffer localStringBuffer = new StringBuffer("");
	    char c = '\'';
	    while (true)
	    {
	      int i = paramString.indexOf(c);
	      if (i >= 0)
	      {
	        localStringBuffer.append(paramString.substring(0, i + 1));
	        localStringBuffer.append(c);
	        paramString = paramString.substring(i + 1, paramString.length());
	      }
	      localStringBuffer.append(paramString);
	      break;
	    }
	    return localStringBuffer.toString();
	  }

	  public static String filterStringFromDB(String paramString)
	  {
	    if ((paramString == null) || (paramString.equals("")))
	      return paramString;
	    return paramString.replace('\16', '\n');
	  }

	  public static String getLangProp(int paramInt)
	  {
	    if (paramInt == 1)
	      return "NLS_LANG";
	    return null;
	  }

	  public static String dbTypeToJavaType(int paramInt)
	  {
	    switch (paramInt)
	    {
	    case 1:
	      return "String";
	    case 2:
	      return "BigDecimal";
	    case 3:
	      return "int";
	    case 4:
	      return "int";
	    case 5:
	      return "int";
	    case 6:
	      return "BigDecimal";
	    case 7:
	      return "BigDecimal";
	    case 8:
	      return "BigDecimal";
	    case 12:
	      return "String";
	    case 91:
	      return "Date";
	    case 92:
	      return "Date";
	    case 93:
	      return "Date";
	    case -1:
	      return "String";
	    case -5:
	      return "int";
	    case -6:
	      return "int";
	    }
	    return "String";
	  }
}
